﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebCarERP.Models;

namespace WebCarERP.Controllers
{
    public class UserController : BaseController
    {
        //
        // GET: /User/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult changePwd()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult changePwd(string password, string confirmPassword, string confirmPassword1)
        {
            //string password_md5New = Md5.Md5Hash(Md5.Md5Hash(entity.password1.Trim().ToUpper()));
            if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(confirmPassword) || string.IsNullOrEmpty(confirmPassword1))
            {
                ModelState.AddModelError("", "抱歉！信息填写不完整，请填写。");
                return View();
            }
            else if (CurrentUserInfo.Pwd != password)
            {
                ModelState.AddModelError("", "原密码不正确！");
                return View();
            }
            else if (password == confirmPassword)
            {
                ModelState.AddModelError("", "原密码不能与新密码一致！");
                return View();
            }

            var info = db.UserInfo.Where(t => t.ID == CurrentUserInfo.ID).FirstOrDefault();
            if (info != null)
            {
                //使用MD5加密
                //if (info.password != entity.password)
                //{
                //    string password_md5 = Md5.Md5Hash(Md5.Md5Hash(entity.password.Trim().ToUpper()));
                //    info.password = password_md5;
                //}
                info.Pwd = confirmPassword;
                db.Entry(info).State = EntityState.Modified;
                db.SaveChanges();
                //if (entity.userid.Equals(CurrentUserInfo.userid))
                //{
                CurrentUserInfo = new RoleUserModel
                {
                    ID = info.ID,
                    UName = info.UName,
                    Pwd = info.Pwd,
                    Phone = info.Phone,
                    Mail = info.Mail,
                    SubTime = info.SubTime,
                    LastModifiedOn = info.LastModifiedOn,
                    DelFlag = info.DelFlag,
                };
                Session["RoleUserModel"] = CurrentUserInfo;
                //}
                string message = "您的信息已成功提交，非常感谢！";
                return Content("<script >alert('" + message + "'); window.location.href = window.location.href;</script >", "text/html");
            }
            ModelState.AddModelError("", "抱歉！没有提交的信息，请填写。");

            return View();
        }
    }
}
